<?php

class LaporangensetController extends Zend_Controller_Action
{

    public function init()
    {
        $this->view->site = $this->getRequest()->getControllerName();
    }

    public function indexAction()
    {
        
    }

    public function calendarAction()
    {
        $this->_helper->getHelper('layout')->disableLayout();
        
    }

    public function proyekAction()
    {
        $this->_helper->getHelper('layout')->disableLayout();
        if(!$this->_request->isXmlHttpRequest())
        {
            $this->_redirect('/laporangenset/');
        } else {
            $proyek = new Application_Model_Proyek();

            $result = $proyek->getAllDataSelect();

            $paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($result));
            $paginator->setItemCountPerPage(3)
                        ->setCurrentPageNumber($this->_getParam('page','1'));

            $this->view->paginator = $paginator;
        }
    }

    public function gensetAction()
    {
        $this->_helper->getHelper('layout')->disableLayout();
        
        if(!$this->_request->isXmlHttpRequest())
        {
            $this->_redirect('/laporangenset/');
        } else {
            $genset = new Application_Model_Genset();
            $genset = $genset->getDataJoin();
            $this->view->genset = $genset;
        }
    }

    public function datacalendarAction()
    {
        
        $this->_helper->getHelper('layout')->disableLayout();
        if(!$this->_request->isXmlHttpRequest())
        {
            $this->_redirect('/laporangenset/');
        } else {
            
            
            $id_proyek = $this->getRequest()->getParam('proyek');
            $id_genset = $this->getRequest()->getParam('genset'); 
            
            if($id_proyek!=null)
            {
                $aktivitas = new Application_Model_Aktivitasharian();
                $aktivitas = $aktivitas->getDatabyIdProyek($id_proyek);
            }
            else if($id_genset!=null){
                $aktivitas = new Application_Model_Aktivitasharian();
                $aktivitas = $aktivitas->getDatabyIdGenset($id_genset);
            }
            else
            {
                $aktivitas = new Application_Model_Aktivitasharian();
                $aktivitas = $aktivitas->getDataJoin();
            }

            $data = array();

            $temp_data = 0;
            $i = 0;
            foreach($aktivitas as $aktivitas)
            {   
                if($aktivitas['tanggal_aktivitas'] == $temp_data) {
                    $i++;
                    if($i <= 1)
                    {
                        $data[] = array(
                        'id' => $aktivitas['id_aktivitas_harian'],
                        'title' => $aktivitas['serial_number_genset'],
                        'start' => $aktivitas['tanggal_aktivitas'],
                        'url' => '#',
                        'color' => '#54c658');
                    } else if ($i == 2)
                    {
                        $data[] = array(
                        'id' => $aktivitas['tanggal_aktivitas'],
                        'title' => 'Lihat lainnya',
                        'start' => $aktivitas['tanggal_aktivitas'],
                        'url' => '#',
                        'color' => '#0fbc5c');

                    }
                } else {
                    $data[] = array(
                        'id' => $aktivitas['id_aktivitas_harian'],
                        'title' => $aktivitas['serial_number_genset'],
                        'start' => $aktivitas['tanggal_aktivitas'],
                        'url' => '#',
                        'color' => '#54c658');
                    $i = 0;
                }

                $temp_data = $aktivitas['tanggal_aktivitas'];
            }

            echo Zend_Json_Encoder::encode($data);
        }
    }

    public function viewAction()
    {
        if(!$this->_request->isXmlHttpRequest())
        {
            $this->_redirect('/laporangenset/');
        }
        
        $this->_helper->getHelper('layout')->disableLayout();
        
        $id = $this->getRequest()->getParam('dataId');
        
        $dateValidator = new Zend_Validate_Date();
        
        if($dateValidator->isValid($id))
        {
            $daftarAktivitas = new Application_Model_Aktivitasharian();
            $result = $daftarAktivitas->getDataJoinbyTgl($id);
            
            $tanggal = new Zend_Date(strtotime($id));
            $tanggal->toString();
            
            $locale = new Zend_Locale('id_ID');
            
            $user = new Application_Model_User();
            $data = array();
            $i = 1;
            
            foreach($result as $userDaftar)
            {
                $data[] = $user->getDataById($userDaftar['id_user']);
            }

            $this->view->tanggal = $tanggal->get('EEEE, dd MMMM YYYY', $locale);
            
            $this->view->daftarAktivitas = $result;
            $this->view->daftarPegawai = $data;
            
        } else {
            $aktivitas = new Application_Model_Aktivitasharian();
            $aktivitas = $aktivitas->getDataJoinbyId($id);
            
            $id_genset = $aktivitas['id_genset'];
            
            $form = new Application_Form_AktivitasGensetForm();

            $form->getSubForm('gensetSummary')->removeDecorator('form');
            $form->getSubForm('gensetSummary')->removeDecorator('htmlTag');

            $form->getSubForm('keadaanGenset')->removeDecorator('form');
            $form->getSubForm('keadaanGenset')->removeDecorator('htmlTag');

            $form->getSubForm('pemeriksaanUmum')->removeDecorator('form');
            $form->getSubForm('pemeriksaanUmum')->removeDecorator('htmlTag');

            $formGensetSummary = $form->getSubForm('gensetSummary');
            
            $formGensetSummary->getElement('id_waktu_pemeriksaan')->setAttrib('disabled', true);
            $formGensetSummary->getElement('jam_operasi')->setAttrib('disabled', true);
            $formGensetSummary->getElement('id_status')->setAttrib('disabled', true);
                              
            $formKeadaanGenset = $form->getSubForm('keadaanGenset');
            
            $formKeadaanGenset->getElement('kondisi')->setAttrib('disabled', true);
            $formKeadaanGenset->getElement('deskripsi')->setAttrib('disabled', true);
            
            $formPemeriksaanUmum = $form->getSubForm('pemeriksaanUmum');
            
            $formPemeriksaanUmum->getElement('kw')->setAttrib('disabled', true);
            $formPemeriksaanUmum->getElement('fuel_press')->setAttrib('disabled', true);
            $formPemeriksaanUmum->getElement('oil_press')->setAttrib('disabled', true);
            $formPemeriksaanUmum->getElement('jw_temp')->setAttrib('disabled', true);
            $formPemeriksaanUmum->getElement('oil_temp')->setAttrib('disabled', true);
            $formPemeriksaanUmum->getElement('fuel_meter')->setAttrib('disabled', true);
            $formPemeriksaanUmum->getElement('cos_q')->setAttrib('disabled', true);
            
            
            $formSuhuKeluaran = $form->getSubForm('suhuKeluaran');
                        
            $gensetModel = new Application_Model_Genset();
            $result = $gensetModel->getDataJoinId($id_genset);
            
            $id_hasil_pemeriksaan = $aktivitas['id_hasil_pemeriksaan'];
            
            $suhu_silinder = new Application_Model_DetailSilinder();
            $resultSilinder = $suhu_silinder->getDataJoin($id_hasil_pemeriksaan);
            
            $suhu_stack = new Application_Model_DetailStack();
            $resultStack = $suhu_stack->getDataJoin($id_hasil_pemeriksaan);
            
            $dataSilinder = array();
            $i = 1;
            
            foreach($resultSilinder as $resultSilinder)
            {
                $dataSilinder['suhu_silinder_'.$i] = $resultSilinder['suhu_silinder'];
                $i++;
            }
            
            $dataStack = array();
            $i = 1;
            
            foreach($resultStack as $resultStack)
            {
                $dataStack['suhu_stack_'.$i] = $resultStack['suhu_stack'];
                $i++;
            }
            
            $no_silinder = $result['no_silinder'];
            $no_stack = $result['no_stack'];
            
            $silinder = 1;
            while($no_silinder!=0){
                $formSuhuKeluaran->addElement('text', 'suhu_silinder_'.$silinder,array('label' => 'Silinder Ke '.$silinder,'required' => true));
                $formSuhuKeluaran->getElement('suhu_silinder_'.$silinder)->setAttrib('disabled', true);
                $no_silinder--;
                $silinder++;
            }
            
            $stack = 1;
            while($no_stack!=0){
                $formSuhuKeluaran->addElement('text', 'suhu_stack_'.$stack,array('label' => 'Stack Ke '.$stack, 'required' => true));
                $formSuhuKeluaran->getElement('suhu_stack_'.$stack)->setAttrib('disabled', true);
                $no_stack--;
                $stack++;
            }
            
            $formSuhuKeluaran->removeDecorator('form');
            $formSuhuKeluaran->removeDecorator('htmlTag');
            
            $this->view->suhuSilinder = $formSuhuKeluaran;
            $this->view->gensetSummary = $formGensetSummary;
            $this->view->keadaanGenset = $formKeadaanGenset;
            $this->view->pemeriksaanUmum = $formPemeriksaanUmum;
            
            $tanggal = new Zend_Date(strtotime($aktivitas['tanggal_aktivitas']));
            $tanggal->toString();
            
            $locale = new Zend_Locale('id_ID');
            
            $this->view->tanggal = $tanggal->get('EEEE, dd MMMM YYYY', $locale);
            
            
            $form->populate($aktivitas);
            $form->populate($result);
            $form->populate($dataSilinder);
            $form->populate($dataStack);
        }
            
        $aktivitas = new Application_Model_Aktivitasharian();
        
    }


}











